<?php
/*
  panel页面主要是用来显示目前公司经营的状况，无需收集任何表单数据。这个页面最初由    
*/

define('ACC', true);
require('./include/init.php');
session_start(); 
//print_r($_SESSION);
$userid = $_SESSION['user_id'];

echo '<head><meta charset = "utf-8"></head>';  
//$userid = 26;

$company = new companymodel();
$companyinfo = $company->getcompanyinfo($userid);
$companyid = $companyinfo['company_id'];

echo '<a href = "dashboard1.php">进入新版界面</a><br/>';


echo '<h2>公司名称为:' . $companyinfo['company_name'], '&nbsp|&nbsp';
echo '目前所在财年:第' . $companyinfo['company_y'] . '财年', '&nbsp|&nbsp';
echo '目前所在季度:第' . $companyinfo['company_q'] . '季度', '</h2><br/><br/>';

echo '&nbsp&nbsp<a href="showactionlog.php">经营日志及原始凭证</a>&nbsp&nbsp<a href="showdailybooking.php">记账凭证</a>&nbsp&nbsp<a href="account_balance.php">分类账簿</a>&nbsp&nbsp<a href="financial_reports.php">财务报表</a><br/><br/>';

$cashinfo = $company->getcash_equivalent($companyid);
echo '<h3>现金及现金等价物：';
foreach($cashinfo as $k=>$v){
echo '<' . $k . '>' . $v . '|';
}
echo '&nbsp&nbsp<a href="capitalinjection.php">资本注入</a></h3>';
echo '<a href="applyforloan.php">申请贷款</a>';

$loaninfo = $company->getloan($companyid);
echo '<h3>目前贷款：';
foreach($loaninfo as $k=>$v){
echo '<期限：' . $v['duration'] . '个季度>' . $v['current_value'] . '|';
}
echo '</h3>'; 

echo '<h3>目前原材料库存：';
echo '入库：';
$rawmaterialinstockinfo = $company->getrawmaterialinstock($companyid);
foreach($rawmaterialinstockinfo as $k=>$v){
echo '<' . $k . '>' . $v . '个|';
}
echo '&nbsp&nbsp&nbsp在途：';
$rawmaterialonthewayinfo = $company->getrawmaterialontheway($companyid);
foreach($rawmaterialonthewayinfo as $k=>$v){
echo '<' . $k . '>' . $v . '个|';
}
echo '<a href="purchase.php?name=raw_material">购买原材料</a>&nbsp&nbsp<a href = "updaterawmaterial.php">更新在途原材料<a></h3>';

echo '<h3>目前成品库存：';
$finishedgoodinfo = $company->getfinishedgood($companyid);
foreach($finishedgoodinfo as $k=>$v){
echo '<' . $k . '>' . $v . '个<form action="selltothemarket.php?elementname=' . $k . '" method="post">&nbsp卖出<input type = "text" name="num"/>个<input type="submit" value="提交"/></form>';
} 
echo '</h3>';

echo '<a href="showorder.php">查看销售订单</a><br/>';

 
echo '<a href = "updateunfinishedequipment.php">更新在建生产线<a>&nbsp&nbsp<a href = "updateunfinishedgood.php">更新在制品</a>';
$buildinginfo = $company->getallelements($companyid, 'building');
foreach($buildinginfo as $k=>$v){
    $elementinfo = $company->getelementinfo($v['building_tagid'], 'building');
    $buildingname = $elementinfo['element_name'];
    $numofpositions = $elementinfo['building_size'];
    if($v['construct_period'] ==  0){
        $buildingstatus = '投入使用';
    }else{
        $buildingstatus = '正在建设';
    }
    echo '<h3>厂房：' . $buildingname . '&nbsp&nbsp&nbsp状态：' . $buildingstatus . '</h3>';
    for ($i = 1; $i <= $numofpositions; $i++){
        $equipmentinfo = $company->getequipment($v['building_tagid'], $i);
        if(empty($equipmentinfo)){
            echo '&nbsp&nbsp&nbsp生产线位置' . $i . ':无生产线。&nbsp&nbsp采购生产线：';
            //列出采购生产线的list以及链接
            $equipmentlist = $company->getelementlist('equipment');
            echo '<form action = "purchaseAct.php" method = "post">';
            echo '<input type = "hidden" name = "type" value = "equipment">';
            echo '<input type = "hidden" name = "building_tagid" value = "' . $v['building_tagid'] . '">';
            echo '<input type = "hidden" name = "position" value = "' . $i . '">';
            echo '<select name = "element_id">';
  
            foreach($equipmentlist  as $key=>$value){
                $equipmentname = $value['element_name'];
                $element_id = $value['element_id'];
                $equipmentprice =  $value['equipment_price'];
                echo '<option value = "'. $element_id  . '">'. $equipmentname . ' 价格：' . $equipmentprice . ' </option> ';
            }
            
            echo '</select><input type = "submit" name = "submit" value = "提交"></form>'; 
            
            echo '<br/>';                    
        }else{
            $elementinfo = $company->getelementinfo($equipmentinfo['equipment_tagid'], 'equipment');
            $equipmentname = $elementinfo['element_name'];
            if($equipmentinfo['installation_period'] ==  0){
                $equipmentstatus = '投入使用';
                $unfinishedgoodinfo = $company->getunfinishedgood($equipmentinfo['equipment_tagid']);
                if(empty($unfinishedgoodinfo)){
                    $workstatus = '生产线空闲中';
                }else{
                    $elementinfo = $company->getelementinfo($unfinishedgoodinfo['product_tagid'], 'product');
                    $unfinishedgood_name = $elementinfo['element_name'];
                    $produceperiod = $unfinishedgoodinfo['produce_period'];
                    $workstatus = '目前有一个<' . $unfinishedgood_name . '>正在生产，还有' . $produceperiod . '个季度完成生产。'; 
                }
                
                echo '&nbsp&nbsp&nbsp生产线位置' . $i . ':' . $equipmentname . '&nbsp&nbsp&nbsp状态：'  .  $equipmentstatus . '&nbsp&nbsp&nbsp工作状态：' . $workstatus . '<br/>'; 
                
            }else{
                $equipmentstatus = '仍在安装';
                echo '&nbsp&nbsp&nbsp生产线位置' . $i . ':' . $equipmentname . '&nbsp&nbsp&nbsp状态：'  .  $equipmentstatus . '<br/>';     
            } 
        }
    }
       
}
 echo '<h3>&nbsp<a href="purchase.php?name=building">投资建设新厂房</a></h3>';

    //echo '<a href="purchase.php?name=equipment">购买新生产线</a>';        
   
?>
